package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EditPasswordDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    private final EmployeeService employeeService;

    private final JwtProperties jwtProperties;

    @Autowired
    public EmployeeController(EmployeeService employeeService, JwtProperties jwtProperties) {
        this.employeeService = employeeService;
        this.jwtProperties = jwtProperties;
    }

    /**
     * 新增员工
     *
     * @param employeeDTO 员工信息
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增员工")
    public Result<String> addEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}", employeeDTO);
        employeeService.addEmployee(employeeDTO);

        return Result.success("新增成功", "");
    }

    /**
     * 分页查询员工
     * @param employeePageQueryDTO 查询条件
     * @return 分页查询结果
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询员工")
    public Result<PageResult<Employee>> pageQueryEmployee(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("分页查询员工：{}", employeePageQueryDTO);
        PageResult<Employee> pageResult = employeeService.pageQueryEmployee(employeePageQueryDTO);

        return Result.success("查询成功", pageResult);
    }

    /**
     * 登录
     *
     * @param employeeLoginDTO 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success("登陆成功",employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return 退出结果
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工退出")
    public Result<String> logout() {
        return Result.success("退出成功", "");
    }

    /**
     * 修改员工状态
     *
     * @param id     员工id
     * @param status 员工状态
     * @return 修改结果
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "修改员工状态")
    public Result<String> updateStatus(Long id, @PathVariable Integer status) {
        log.info("修改员工状态：id={}, status={}", id, status);
        employeeService.updateStatus(id, status);

        return Result.success("修改成功", "");
    }

    /**
     * 更新员工
     *
     * @param employeeDTO 员工信息
     * @return 更新结果
     */
    @PutMapping
    @ApiOperation(value = "更新员工")
    public Result<String> updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("更新员工：{}", employeeDTO);
        employeeService.updateEmployee(employeeDTO);

        return Result.success("更新成功", "");
    }

    /**
     * 根据员工id查询员工
     *
     * @param id 员工id
     * @return 员工信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据员工id查询员工")
    public Result<Employee> selectEmployeeById(@PathVariable Long id) {
         Employee employee = employeeService.selectEmployeeById(id);
        return Result.success("查询成功", employee);
    }

    /**
     * 修改密码
     * @param editPasswordDTO 修改密码信息
     * @return 修改结果
     */
    @PutMapping("/editPassword")
    @ApiOperation(value = "修改密码")
    public Result<String> editPassword(@RequestBody EditPasswordDTO editPasswordDTO) {
        log.info("修改密码：{}", editPasswordDTO);
        employeeService.editPassword(editPasswordDTO);
        return Result.success("修改成功", "");
    }
}
